package top.imyzt.controller;


import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.imyzt.entity.TUser;
import top.imyzt.service.ITUserService;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author imyzt
 * @since 2018-01-05
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户操作", value = "/user")
public class TUserController extends BaseController{

	private Logger log = Logger.getLogger(TUserController.class); 

	@Autowired
	private ITUserService userService;

	//api方法注释
	@ApiOperation(value = "获取用户列表")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		List<TUser> list = userService.selectList(null);
		if (list != null) {
			return retContent(200, list);
		}
		return retContent(1100, list);
	}

	@ApiOperation(value = "创建用户")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insertUser(@ModelAttribute TUser user) {
		
		if (user != null) {
			if (userService.insert(user)) {
				return retContent(1101, null);
			}
			return retContent(1102, null);
		}
		return retContent(1200, null);
	}

	@ApiOperation(value = "查看用户详情")
	@RequestMapping(value = "/selById/{id}", method = RequestMethod.GET)
	public String selById(@PathVariable(value = "id") Integer id) {
		
		if (id != null) {
			TUser user = userService.selectById(id);
			if (user != null) {
				return retContent(1112, user);
			}
			return retContent(1100, null);
		}
		return retContent(1200, null);
	}

	@ApiOperation(value = "修改用户")
	@RequestMapping(value = "/updateById", method = RequestMethod.PUT)
	public String updateById(@ModelAttribute TUser user) {
		if (user != null) {
			if (userService.updateById(user)) {
				return retContent(1113, null);
			}
			return retContent(1114, user);
		}
		return retContent(1200, null);
	}
	
	@ApiOperation(value = "删除用户")
	@RequestMapping(value = "/updateById/{id}", method = RequestMethod.DELETE)
	public String deleteById(@PathVariable(value = "id") Integer id) {
		
		if (id != null) {
			if (userService.deleteById(id)) {
				return retContent(1115, null);
			}
			return retContent(1116, null);
		}
		return retContent(1200, null);
	}
}

